package com.weisw.polaris.admin.controller;

import com.weisw.polaris.admin.model.Menu;
import com.weisw.polaris.admin.model.User;
import com.weisw.polaris.admin.service.impl.MenuServiceImpl;
import com.weisw.polaris.admin.service.impl.UserServiceImpl;
import com.weisw.polaris.admin.util.PasswordUtils;
import com.weisw.polaris.core.http.HttpResult;
import com.weisw.polaris.core.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户控制器
 *
 * @author Louis
 * @date Jan 13, 2019
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuServiceImpl menuService;

    @PostMapping(value = "/save")
    public HttpResult save(@RequestBody Menu record) {
        if (record.getMenuTitle() == null){
            return HttpResult.error("请输入菜单名");
        }
        Menu menu = menuService.findById(record.getMenuId());
        if (menu == null) {
            // 新增菜单
            if (menuService.findByName(record.getMenuTitle()) != null) {
                return HttpResult.error("菜单已存在!");
            }

        }
        return HttpResult.ok(menuService.save(record));
    }

    @PostMapping(value = "/delete")
    public HttpResult delete(@RequestBody List<Menu> records) {
        return HttpResult.ok(menuService.delete(records));
    }

//    @PostMapping(value = "/getMenu")
//    public HttpResult getMenuTree(@RequestParam(value = "当前角色菜单", required = false) List menuList) {
//        System.out.println(id);
//        return HttpResult.ok(menuService.getMenu(menuList));
//    }

    @GetMapping(value = "/findByName")
    public HttpResult findByUserName(@RequestParam String name) {
        return HttpResult.ok(menuService.findByName(name));
    }

    @PostMapping(value = "/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(menuService.findPage(pageRequest));
    }

}
